// pages/check/rescue/sure/sure.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		list: []
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		wx.getStorage({
			key:'uid',
			success:(e)=>{
				this.setData({
					id:options.id,
					uid:e.data.uid
				})
			}
		})
	},
	sure(){
		wx.showToast({
			title: '加载中',
			icon: 'loading'
		});
		let that=this
		if(this.data.list.length==0){
			wx.showToast({
				title:'请上传图片',
				icon:'none'
			})
		}else{
			var promise = new Promise(function(resolve, reject) {
				let nat=[]
				that.data.list.forEach(function(item, index) {
					wx.uploadFile({
						url: getApp().globalData.http3,
						filePath: item,
						name: 'picturePath',
						success: function(res) {
							let c = JSON.parse(res.data)
							nat.push(c.data.headImage)
						}
					});
				})
				setTimeout(function(){resolve(nat);},1000)
			}).then(function(nat){
				nat=nat.join()
				console.log(nat)
				wx.request({
					url:getApp().globalData.chang+'action=doJyorder&id='+that.data.id+'&pic='+nat+'&uid='+that.data.uid,
					success: (e) => {
						console.log(e)
						let pages = getCurrentPages(); // 当前页的数据，可以输出来看看有什么东西
						let prevPage = pages[pages.length - 2];
						console.log(prevPage.route)
						if(e.data.code==1){
							if(prevPage.route=='pages/check/rescue/list/list'){
								wx.navigateBack({
									delta:2
								})
							}else{
								wx.navigateBack({
									delta:1
								})
							}
						}else{
							wx.showToast({
								icon:'none',
								title:e.data.msg
							})
						}
					}
				})
				wx.hideToast();
			})
		}
	},
	sc() {
		let a = this.data.list
		if (a.length < 9) {
			let nan=9-a.length
			let that = this
			wx.chooseImage({
				count: nan,
				sizeType: 'compressed',
				success: (e) => {
					e.tempFilePaths.forEach(function(item, index) {
						a.push(e.tempFilePaths[index])
					})
					that.setData({
						list: a
					})
				}
			})
		}
	},
	dete(e) {
		let a = this.data.list
		a.splice(e.currentTarget.dataset.id, 1)
		this.setData({
			list: a
		})
	},
})
